//
//  MessageNotificationViewController.m
//  StormWatch
//
//  Created by 王岩 on 2024/9/18.
//

#import "MessageNotificationViewController.h"
#import "NotificationManagerCell.h"
#import "MessageNotificationCell.h"

@interface MessageNotificationViewController ()
/* <#note#> */
@property (nonatomic, assign) BOOL notificationOpen;/* <#note#> */
@property (nonatomic, strong) NSMutableArray * openListArr;
@property (nonatomic, strong) NSArray * imageArr;
@end

@implementation MessageNotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = SWString(@"Notification");
    self.openListArr = [[NSMutableArray alloc] init];
    [self readData];
    // Do any additional setup after loading the view from its nib.
}

- (void)readData {
    [[StormCommandTool sharedInstance] readStormCommand:Command_Notification reBack:^(NSString * _Nonnull backData) {
        //  01 0f000000 00
        if (backData.length > 26) {
            NSString * valueStr = [backData substringFromIndex:26];
            self.notificationOpen = ![[valueStr substringToIndex:2] isEqualToString:@"00"];
            
            [self updateUI:valueStr];
        }
    }];
}

- (void)updateUI:(NSString *)valueStr {
    for (int i = 1; i <= 4; i++) {
        NSString * s1 = [NSString stringWithFormat:@"%lld",strtoll([[NSString stringWithFormat:@"0x%@",[valueStr substringWithRange:NSMakeRange(2 * i, 2)]] UTF8String], 0, 16)];
        NSString *byte1 = [SWTool decimalToBinary:s1];
        
        for (int j = 7; j >= 0; j --) {
            BOOL b =  [[byte1 substringWithRange:NSMakeRange(j, 1)] isEqualToString:@"1"];
            [self.openListArr addObject:@(b)];
        }
    }
    
    [self.tableView reloadData];
}

- (void)initData {
    [self.dataArr addObjectsFromArray:@[
        SWString(@"Phone"),
        SWString(@"Messages"),
        SWString(@"QQ"),
        SWString(@"WeChat"),
        SWString(@"Weibo"),
        SWString(@"Mailbox"),
        SWString(@"WeChat Work"),
        SWString(@"Facebook"),
        SWString(@"WhatsApp"),
        SWString(@"YouTube"),
        SWString(@"Skype"),
        SWString(@"Twitter"),
        SWString(@"Instagram"),
        SWString(@"LinkedIn"),
        SWString(@"NaverCafe"),
        SWString(@"Between"),
        SWString(@"KakaoTalk"),
        SWString(@"Band"),
        SWString(@"Gmail"),
        SWString(@"NetFlix"),
        SWString(@"Telegram"),
        SWString(@"Line"),
        SWString(@"163邮箱"),
        SWString(@"hotmail"),
        SWString(@"outlook"),
        SWString(@"snapahat"),
        SWString(@"TOM"),
        SWString(@"Yahoo"),
        SWString(@"Other")]];
    
    self.imageArr = @[
        @"icon电话",
        @"icon短信",
        @"iconqq",
        @"icon微信",
        @"icon微博",
        @"iconmailbox",
        @"icon企业微信",
        @"iconfacebook",
        @"iconwhats",
        @"iconyoutubu",
        @"iconskype",
        @"icontwitter",
        @"iconins",
        @"iconlinkedin",
        @"iconnavercafe",
        @"iconbetween",
        @"iconkakao",
        @"iconband",
        @"icongmail_",
        @"iconnet",
        @"icontelegram",
        @"iconline",
        @"icon网易邮箱",
        @"iconhotmail",
        @"iconoutlook",
        @"iconsnapchat",
        @"icontom",
        @"iconyahoo",
        @"icon其他信息",
    ];
}

- (void)initUI {
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight - kHomeIndicatorHeight);
    [self.tableView registerNib:[UINib nibWithNibName:@"NotificationManagerCell" bundle:nil] forCellReuseIdentifier:@"NotificationManagerCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MessageNotificationCell" bundle:nil] forCellReuseIdentifier:@"MessageNotificationCell"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.notificationOpen) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NotificationManagerCell * cell = [tableView dequeueReusableCellWithIdentifier:@"NotificationManagerCell" forIndexPath:indexPath];
        cell.swithBtn.on = self.notificationOpen;
        cell.changeBlock = ^(BOOL value) {
            self.notificationOpen = value;
            [self.tableView reloadData];
            [self writeData];
        };
        return cell;
    }else {
        MessageNotificationCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MessageNotificationCell" forIndexPath:indexPath];
        cell.nameLab.text = self.dataArr[indexPath.row];
        cell.swithBtn.on = [self.openListArr[indexPath.row] boolValue];
        cell.iconView.image = [UIImage imageNamed:self.imageArr[indexPath.row]];
        cell.changeBlock = ^(BOOL value) {
            [self.openListArr replaceObjectAtIndex:indexPath.row withObject:@(value)];
            [self.tableView reloadData];
            [self writeData];
        };
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return UITableViewAutomaticDimension;
    }
    return 70;
}

- (void)writeData {
    NSMutableArray * copyData = [[NSMutableArray alloc] initWithArray:self.openListArr];
    /// 交换数据位置
    for (int i = 0; i < 4; i ++) {
        [copyData exchangeObjectAtIndex:i withObjectAtIndex:7 - i];
        [copyData exchangeObjectAtIndex:i + 8 withObjectAtIndex:8 + 7 - i];
        [copyData exchangeObjectAtIndex:i + 16 withObjectAtIndex:16 + 7 - i];
        [copyData exchangeObjectAtIndex:i + 24 withObjectAtIndex:24 + 7 - i];
    }
        
    NSString * str = [copyData componentsJoinedByString:@""];
    long byte1 = strtol([[str substringWithRange:NSMakeRange(0, 8)] UTF8String], NULL, 2);
    long byte2 = strtol([[str substringWithRange:NSMakeRange(8, 8)] UTF8String], NULL, 2);
    long byte3 = strtol([[str substringWithRange:NSMakeRange(16, 8)] UTF8String], NULL, 2);
    long byte4 = strtol([[str substringWithRange:NSMakeRange(24, 8)] UTF8String], NULL, 2);

    NSArray * dataArr = @[@(self.notificationOpen),@(byte1),@(byte2),@(byte3),@(byte4)];
    
    [StormCommandTool.sharedInstance writeStormCommand:Command_Notification Value:dataArr reBack:^(NSString * _Nonnull backData) {
        if (backData.length > 26) {
            NSString * valueStr = [backData substringFromIndex:26];
            self.notificationOpen = ![[valueStr substringToIndex:2] isEqualToString:@"00"];
            toast(SWString(@"Success"));
            [self updateUI:valueStr];
        }
    }];
 
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
